package top.frsf.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.frsf.reggie.common.BaseContext;
import top.frsf.reggie.common.R;
import top.frsf.reggie.dto.OrdersDto;
import top.frsf.reggie.entity.Orders;
import top.frsf.reggie.service.OrdersService;

/**
 * @Author FRSF
 * @Date 2023/1/29 23:32
 * @Description 订单
 */

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;


    /**
     * @Author: FRSF
     * @Date: 2023/1/29 23:39
     * @Description 用户下单
     * @param orders:
     * @return: top.frsf.reggie.common.R<java.lang.String>
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("提交订单数据:{}", orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }


    /**
     * @Author: FRSF
     * @Date: 2023/2/12 20:55
     * @Description 移动端订单分页查询
     * @param page:
     * @param pageSize:
     * @return: top.frsf.reggie.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page < top.frsf.reggie.entity.Orders>>
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> pageOrders(int page, int pageSize) {
        // 基础订单信息查询
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        wrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage, wrapper);

        // 订单Dto封装
        Page<OrdersDto> pageOrdersDto = new Page<>();
        ordersService.pageByDto(ordersPage, pageOrdersDto);
        return R.success(pageOrdersDto);
    }

    /**
     * @Author: FRSF
     * @Date: 2023/2/12 21:41
     * @Description 后台订单分页数据
     * @param page:
     * @param pageSize:
     * @return: top.frsf.reggie.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page<top.frsf.reggie.entity.Orders>>
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        ordersService.page(ordersPage);
        return R.success(ordersPage);
    }

}
